package com.example.emoiticon;

import java.io.IOException;

import com.example.emoiticon.adapter.GifAnimationDrawable;
import com.example.emoiticon.adapter.ImageAdapter;
import com.example.emoiticon.adapter.SessionManager;

import android.R.integer;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources.NotFoundException;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;




public class SecondActivity extends Fragment{
	//private ImageView imageview;
	private GridView gridview;
	ImageAdapter imgadapter;
	ImageView imageselect;
	Button btdel;
	private GifAnimationDrawable big;
	 public int[] imagesIds = {R.raw.tedy0,R.raw.tedy1,R.raw.tedy2,R.raw.tedy3,
			  R.raw.tedy4,R.raw.tedy5,R.raw.tedy6,R.raw.tedy7,
			  R.raw.tedy8,R.raw.tedy9,R.raw.tedy10,R.raw.tedy11,
			  R.raw.tedy12};
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
	        View v = inflater.inflate(R.layout.animation_grid, null);
	        imageselect=(ImageView)v.findViewById(R.id.imageselect);
	        gridview=(GridView)v.findViewById(R.id.gridanimation);
	        btdel=(Button)v.findViewById(R.id.btdelete);
	        btdel.setVisibility(v.GONE);
	        imageselect.setVisibility(v.GONE);
	        btdel.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					imageselect.setImageDrawable(null);
					SessionManager s=new SessionManager(SecondActivity.this.getActivity());
					s.setUserData(0);
					btdel.setVisibility(v.GONE);
					imageselect.setVisibility(v.GONE);
				}
			});
	        new Handler().post(new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					imgadapter=new ImageAdapter(getActivity().getApplicationContext(),imagesIds);
					gridview.setAdapter(imgadapter);
					
				}
			});
	        gridview.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						final int position, long id) {
					// TODO Auto-generated method stub
				    btdel.setVisibility(view.VISIBLE);
				    imageselect.setVisibility(view.VISIBLE);
					 try {
						   // i = (int) imgadapter.getItemId(position);
							big = new GifAnimationDrawable(SecondActivity.this.getActivity().getApplicationContext().getResources().openRawResource(imagesIds[position]));
							///Toast.makeText(FistActivity.this.getActivity().getApplicationContext(), Integer.toString(imagesIds[position]),Toast.LENGTH_SHORT).show();
							imageselect.setImageDrawable(big);
							big.start();
							big.setVisible(true, true);	
						
							imageselect.post(new Runnable() {
								
								@SuppressLint("NewApi") @Override
								public void run() {
									// TODO Auto-generated method stub
									GifAnimationDrawable gif=(GifAnimationDrawable)imageselect.getDrawable();
									gif.start();
									/*Intent intent = new Intent(getActivity().getApplicationContext(),MainActivity.class);
									intent.putExtra("image",imagesIds[position]);
								    startActivity(intent);*/
									
								}
							});
							/*Intent intent = new Intent(getActivity().getApplicationContext(), MainActivity.class);
							Bundle b = new Bundle();
							
							b.putInt("image",imagesIds[position]); //Your id
							//intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
							intent.putExtras(b); //Put your id to your next Intent
							startActivity(intent);
							getActivity().finish();*/
							SessionManager s=new SessionManager(getActivity());
							s.setUserData(imagesIds[position]);
						/*	Intent intent = new Intent(getActivity(), MainActivity.class);
				            startActivity(intent);
				            getActivity().finish();*/
							
							
						
					
							//((GiftedymationDrawable)imageview.getDrawable()).setVisible(true, true);
					 } catch (NotFoundException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}			
							
				
					
					
				}
			});
	        
	        return v;

}
}
